﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Edit_CustomClearance : Form
    {
        public frm_Edit_CustomClearance()
        {
            InitializeComponent();            
            this.mySettings = new INISettings();
            this.myConnection = new SqlConnection(mySettings.SQLConnectionString);
        }       

        public bool LoadData(string id)
        {
            this.ClearanceID = id;
            if (this.ClearanceID != "")
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM \"241_CustomClearance\" WHERE ID='" + this.ClearanceID + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.clearanceNo.Text = dataReader["ID"].ToString();
                        DateTime register;
                        DateTime.TryParse(dataReader["RegisterDate"].ToString(), out register);
                        this.registeredDate.Text = register.ToShortDateString();
                        DateTime finish;
                        DateTime.TryParse(dataReader["FinishedDate"].ToString(), out finish);
                        this.finishedDate.Text = finish.ToShortDateString();
                        this.noteMemo.Text = dataReader["Note"].ToString();
                    }
                    dataReader.Close();
                    myConnection.Close();
                    return true;
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot get Data for Clearance No: " + ClearanceID);
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            
            if (ClearanceID != clearanceNo.Text)
            {
                //The Clearance No. changed
                
                bool duplicate = false;
                
                //Check Duplicate
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM \"241_CustomClearance\" WHERE ID='" + clearanceNo.Text + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        duplicate = true;
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    duplicate = true;
                }

                if (duplicate)
                {
                    MessageBox.Show("The Clearance No. " + clearanceNo.Text + " existed");
                }
                else
                {
                    try
                    {
                        DateTime register;
                        DateTime.TryParse(registeredDate.DateTime.Date.ToString(), out register);
                        DateTime finish;
                        DateTime.TryParse(finishedDate.DateTime.Date.ToString(), out finish);
                        myConnection.Open();
                        string sql = "UPDATE \"241_CustomClearance\" "
                                + "SET ID='" + clearanceNo.Text + "', "
                                + "RegisterDate=@register, "
                                + "FinishedDate=@finished, "
                                + "Note='" + noteMemo.Text + "' "
                                + "WHERE ID='" + ClearanceID + "'";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.Parameters.Add("@register", SqlDbType.DateTime).Value = register.Date;
                        cmd_sql.Parameters.Add("@finished", SqlDbType.DateTime).Value = finish.Date;
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();                        
                        myConnection.Close();
                        MessageBox.Show("Update Success!");
                        this.Dispose();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Update failed!");
                    }
                }
            }
            else
            {
                //The Clearance No. not changed
                try
                {
                    DateTime register;
                    DateTime.TryParse(registeredDate.DateTime.Date.ToString(), out register);
                    DateTime finish;
                    DateTime.TryParse(finishedDate.DateTime.Date.ToString(), out finish);
                    myConnection.Open();
                    string sql = "UPDATE \"241_CustomClearance\" "
                            + "SET RegisterDate=@register, "
                            + "FinishedDate=@finished, "
                            + "Note='" + noteMemo.Text + "' "
                            + "WHERE ID='" + ClearanceID + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.Parameters.Add("@register", SqlDbType.DateTime).Value = register.Date;
                    cmd_sql.Parameters.Add("@finished", SqlDbType.DateTime).Value = finish.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Update Success!");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Update failed!");
                }
            }
        }
    }
}
